<template>
  <el-dialog
    title="编辑节点"
    :visible.sync="dialogVisible"
    width="30%"
    :close-on-click-modal="false"
    :close-on-press-escape="false"
  >
    <el-form :model="formData" :rules="rules" label-width="80px" ref="form">
      <el-form-item label="接口" prop="jkbh">
        <el-select v-model="formData.jkbh">
          <el-option
            v-for="item in jkList"
            :key="item.jkbh"
            :value="item.jkbh"
            :label="item.jkmc"
          />
        </el-select>
      </el-form-item>
    </el-form>

    <span slot="footer" class="dialog-footer">
      <el-button @click="dialogVisible = false">取消</el-button>
      <el-button type="primary" @click="saveJk">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
import jkglService from "@/api/jkgl";
export default {
  data() {
    return {
      // 是否显示弹窗
      dialogVisible: false,
      // 接口列表
      jkList: [],
      // 接口表单数据
      formData: {
        jkbh: ""
      },
      // 表单校验规则
      rules: {
        jkbh: [{ required: true, message: "请选择接口", trigger: "change" }]
      }
    };
  },
  mounted() {
    this.getJkList();
  },
  methods: {
    // 打开弹窗
    openDialog(data) {
      this.dialogVisible = true;
      this.$refs.form?.resetFields();
      this.$nextTick(() => {
        this.formData.jkbh = data?.jkbh || "";
      });
    },
    // 获取接口列表
    getJkList() {
      const params = {
        filter: {},
        orderBy: "",
        pageNum: 1,
        pageSize: 10000
      };
      jkglService
        .queryWfbdjk(params)
        .then(res => {
          this.jkList = res.list;
        })
        .catch(err => {
          !err.isError &&
            this.$message({ type: "warning", message: err.message });
        });
    },
    // 保存接口
    saveJk() {
      this.$refs.form?.validate(valid => {
        if (valid) {
          const jk = this.jkList.find(item => item.jkbh === this.formData.jkbh);
          this.$emit("updateLct", {
            jkbh: this.formData.jkbh,
            jkmc: jk?.jkmc || ""
          });
          this.dialogVisible = false;
        }
      });
    }
  }
};
</script>

<style lang="scss" scoped>
.el-select {
  width: 100%;
}
</style>
